//
//  ACAssetModel.m
//  AlbumFetch
//
//  Created by 黄志明 on 2017/2/6.
//  Copyright © 2017年 黄志明. All rights reserved.
//

#import "ACAssetModel.h"
#import <Photos/Photos.h>
#import <AssetsLibrary/AssetsLibrary.h>

@implementation ACAssetModel
@synthesize width = _width,
            height = _height,
            localIdentifier = _localIdentifier,
            duration = _duration;

+ (instancetype)modelWithAsset:(id)asset inAlbum:(NSString *)albumId type:(ACAssetModelType)type
{
    ACAssetModel *assetModel = [[ACAssetModel alloc] init];
    assetModel.asset = asset;
    assetModel.modelType = type;
    assetModel.albumIdentifier = albumId;
    
    if ([asset isKindOfClass:[PHAsset class]])
    {
        [assetModel getAssetPrivateProperty];
    }
    
    return assetModel;
}

- (void)getAssetPrivateProperty
{
    CGFloat scale = 1;//[UIScreen mainScreen].scale;
    if (self.modelType == ACAssetModelTypePhoto)
    {
        if ([_asset isKindOfClass:[PHAsset class]])
        {
            PHAsset *pAsset = (PHAsset *)_asset;
            
            _width = pAsset.pixelWidth / scale;
            _height = pAsset.pixelHeight / scale;
            
            _localIdentifier = pAsset.localIdentifier;
        }
        else if ([_asset isKindOfClass:[ALAsset class]])
        {
            ALAssetRepresentation *assetRepresentation = [(ALAsset*)_asset defaultRepresentation];
            
            CGSize dimension = [assetRepresentation dimensions];
            _width = dimension.width;
            _height = dimension.height;
            
            ALAsset *alAsset = (ALAsset *)_asset;
            _localIdentifier = [alAsset valueForProperty:ALAssetPropertyAssetURL];
            
        }
    }
    else if (self.modelType == ACAssetModelTypeVideo)
    {
        if ([_asset isKindOfClass:[PHAsset class]])
        {
            PHAsset *pAsset = (PHAsset *)_asset;
            
            _width = pAsset.pixelWidth / scale;
            _height = pAsset.pixelHeight / scale;
            
            _localIdentifier = pAsset.localIdentifier;
            
            _duration = pAsset.duration;
        }
        else if ([_asset isKindOfClass:[ALAsset class]])
        {
            ALAssetRepresentation *assetRepresentation = [(ALAsset*)_asset defaultRepresentation];
            
            CGSize dimension = [assetRepresentation dimensions];
            _width = dimension.width;
            _height = dimension.height;
            
            ALAsset *alAsset = (ALAsset *)_asset;
            _localIdentifier = [alAsset valueForProperty:ALAssetPropertyAssetURL];
            
            _duration = [[alAsset valueForProperty:ALAssetPropertyDuration] integerValue];
        }
    }
    // 音频的属性，暂时不拿
}

- (NSString *)convertTimeIntervalToString:(NSTimeInterval)duration
{
    NSString *durationLength = @"";
    // 时长大于1小时
    if ((NSInteger)duration > 3600)
    {
        NSInteger hour = duration / 3600,
        minute = (NSInteger)(duration - hour * 3600) % 60;
        
        durationLength = [NSString stringWithFormat:@"%zd:%02zd", hour, minute];
    }
    else
    {
        NSInteger minute = duration / 60,
        second = (NSInteger)duration % 60;
        
        durationLength = [NSString stringWithFormat:@"%zd:%02zd", minute, second];
    }
    
    return durationLength;
}

- (NSUInteger)hash
{
    return [_localIdentifier hash] ^ [_albumIdentifier hash];
}

- (BOOL)isEqual:(id)object
{
    if (![object isKindOfClass:[ACAssetModel class]])
    {
        return  NO;
    }
    
    ACAssetModel *comObject = (ACAssetModel *)object;
    
    if (!self.albumIdentifier ||
        !comObject.albumIdentifier)
    {
        return NO;
    }
    
    if (![self.albumIdentifier isEqualToString:comObject.albumIdentifier]) {
        return NO;
    }
    
    if ([_asset isKindOfClass:[PHAsset class]])
    {
        if (![comObject.asset isKindOfClass:[PHAsset class]]) return NO;
        
        PHAsset *phAsset = (PHAsset *)_asset,
        *comPHAsset = (PHAsset *)comObject.asset;
        
        return [phAsset.localIdentifier isEqualToString:comPHAsset.localIdentifier];
        
    }
    else if ([_asset isKindOfClass:[ALAsset class]])
    {
        if (![comObject.asset isKindOfClass:[ALAsset class]]) return NO;
        
        NSString *oriURL = [[(NSURL *)self.localIdentifier absoluteString] stringByAppendingString:_albumIdentifier],
                 *comURL = [[(NSURL *)comObject.localIdentifier absoluteString] stringByAppendingString:_albumIdentifier];
        
        return [oriURL isEqualToString:comURL];
    }
    
    return NO;
}

#pragma mark - Getter

- (CGFloat)width
{
    if (_width == 0)
    {
        [self getAssetPrivateProperty];
    }
    
    return _width;
}

- (CGFloat)height
{
    if (_height == 0) {
        [self getAssetPrivateProperty];
    }
    
    return _height;
}

- (id)localIdentifier
{
    if (!_localIdentifier)
    {
        [self getAssetPrivateProperty];
    }
    
    return _localIdentifier;
}

- (NSTimeInterval)duration
{
    if (!_duration)
    {
        [self getAssetPrivateProperty];
    }
    
    return _duration;
}


#pragma mark - NSCoding

+ (BOOL)supportsSecureCoding
{
    return YES;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeObject:_localIdentifier forKey:@"_localIdentifier"];
    [aCoder encodeObject:_albumIdentifier forKey:@"_albumId"];
    [aCoder encodeObject:[NSNumber numberWithFloat:_width] forKey:@"_width"];
    [aCoder encodeObject:[NSNumber numberWithFloat:_height] forKey:@"_height"];
    [aCoder encodeObject:[NSNumber numberWithDouble:_duration] forKey:@"_duration"];
    [aCoder encodeObject:[NSNumber numberWithInteger:_modelType] forKey:@"_modelType"];
}

- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];
    if (self)
    {
        _localIdentifier = [aDecoder decodeObjectForKey:@"_localIdentifier"];
        _albumIdentifier = [aDecoder decodeObjectOfClass:[NSString class] forKey:@"_albumId"];
        _modelType = [[aDecoder decodeObjectOfClass:[NSNumber class] forKey:@"_modelType"] integerValue];
        _width = [[aDecoder decodeObjectOfClass:[NSNumber class] forKey:@"_width"] floatValue];
        _height = [[aDecoder decodeObjectOfClass:[NSNumber class] forKey:@"_height"] floatValue];
        _duration = [[aDecoder decodeObjectOfClass:[NSNumber class] forKey:@"_duration"] doubleValue];
    }
    
    return self;
}


@end

@implementation ACAlbumModel


@end
